<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>游记明细</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <link rel="stylesheet" href="/css/travelContent.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <!-- 引入jquery 和 common.js 可以获取到地址栏上拼接的参数 -->
    <script type="text/javascript" src="/js/common.js"></script>
    <script type="text/javascript" src="js/plugins/dialog2/dialog.min.js"></script>
    <script>
        $(function () {
            // 先从地址栏上获取当前游记的i的,发送请求获取对应的id的游记信息,渲染到页面中
            var params = getParams(); // 获取地址栏上的数据信息转成key=value的形式
            if (params.id) {
                // 发送Ajax请求查询对应id的游记内容
                $.get("/travels/" + params.id, function (data) {
                    // 数据渲染
                    $(".travels").renderValues(data, {
                        getHeadImgHref: function (item, value) {
                            var url = $(item).data('href');
                            $(item).attr("href", url + value);
                        }
                    });
                });
            }

            //收藏
            collect();//加载页面时默认调用一次渲染收藏数目的方法
            //查询是否被收藏
            function collect() {
                $.get("/collects/2/" + params.id, function (data) {
                    if (data) {
                        $("#collectBtn").css("color", "red");
                    } else {
                        $("#collectBtn").css("color", "black");

                    }
                });

            };

            //计算收藏的数量
            function countCollect() {
                $.get("/collects/count/2/" + params.id, function (data) {
                    $(".collectBtn").renderValues({collectNum: data});

                })
            }

            countCollect();

            var user = JSON.parse(sessionStorage.getItem("user"));
            $("#collectBtn").click(function () {
                if (user) {
                    $.get("/collects/2" + "/" + params.id, function (data) {
                        if (data) {
                            // 说明用户已经收藏,提示用户,并且取消收藏
                            $.ajax({
                                type: "DELETE",
                                url: "/collects/2" + "/" + params.id,
                                success: function () {
                                    $(document).dialog({
                                        type: 'notice',
                                        infoText: '亲,收藏已经取消成功!',
                                        autoClose: 1000,
                                        position: 'bottom'  // center: 居中; bottom: 底部
                                    });
                                    collect();
                                    countCollect();
                                }
                            })
                        } else {
                            $.post("/collects/2" + "/" + params.id, function (data) {
                                if (data.success) {
                                    $("#collectBtn").css("color", "red");
                                }
                                countCollect();
                            });
                        }
                    });
                } else {
                    // alert("亲,收藏请先登录");
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '请登陆后再执行相关操作!',
                        onClickConfirmBtn: function () {
                            window.location.href = "/login.html"
                        }
                    });
                }
            });

            // 查询评论
            // 当前页变量  / 手机是滑动查询下一页的
            var currentPage = 1;
            var pages;
            var travelArr = [];

            function query() {
                $.get("/articleComment/articles/" + params.id, {currentPage: currentPage}, function (data) {
                     $(".nums").renderValues(data);
                    $.merge(travelArr, data.list);
                    var json = {
                        list: travelArr
                    }
                    var number = json.list.length;
                    $(".comment").renderValues(json, {
                        getHref: getHref
                    });
                    // 获取总页数
                    pages = data.pages;
                });
                currentPage = currentPage + 1;
            }

            query();
            $(window).scroll(function () {
                if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                    // 判断是否到最后一页 / 如果大于最后一页就不发送请求了
                    if (currentPage <= pages) {
                        query();
                    } else {
                        // 已经到最后了就提示用户已经到底了(引入插件)
                        $(document).dialog({
                            type: "notice",  // 好像时设置样式
                            content: "<span class='info-text'>没有更多了!</span>", // 显示的文字
                            autoClose: 1000 // 多久后关闭
                        })
                    }
                }
            })

            // 点击添加评论框
            $("#commentBtn").click(function () {
                if (user) {
                    $("#travelModal").removeClass("modal");
                    $("#travelModal").addClass("operationBottom");
                } else {
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '亲,你还未登陆!是否现在登陆?',
                        onClickConfirmBtn: function () {
                            window.location.href = "/index.html";
                        }
                    });
                }
            });

            // 提交评论
            $("#submitBtn").click(function () {
                // 获取自定义标签内容
                // 打包成参数
                var data = {
                    content: $("[name='content']").val()//获取内容
                };
                // 提交数据
                $.post("/articleComment/" + params.id + "/comment", data, function (data) {
                    // window.location.href = "/strategyCatalogs.html?id=" + 2;
                    window.location.reload();
                });
                $("#travelModal").removeClass("operationBottom");
                $("#travelModal").addClass("modal");
            });
            //取消取消评论按钮
            $("#remove").click(function () {
                $("#travelModal").removeClass("operationBottom");
                $("#travelModal").addClass("modal");
            });
        });
    </script>

</head>
<body>
<div class="search-head operationTop">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
        </div>
    </div>
</div>
<div class="search-head" style="height: 48px;background: white;">
</div>
<div class="travels">
    <img class="cover" render-src="coverUrl"><!--游记的封面-->
    <div class="container">
        <!--游记标题-->
        <h6 class="title" render-html="title"></h6>
        <small>
            <span render-html="releaseTime"></span><!--游记发布时间-->
            浏览 <span> 0</span>
            回复 <span> 0</span>
        </small>
        <div class="row">
            <div class="col-2 img">
                <!--用户头像-->
                <a data-href="userProfiles.html?id=" render-fun="getHeadImgHref" render-key="author.id">
                    <img class="rounded-circle" render-src="author.headImgUrl">
                </a>
            </div>
            <div class="col-6 mine">
                <small>
                    <span render-html="author.nickName"></span><!--用户昵称-->
                    <br>
                    <span>3 </span>篇游记
                    <span>2 </span>粉丝
                </small>
            </div>
            <div class="attention col">
                <span class="addr" render-html="place">广州</span><!--旅游的地址-->
            </div>
        </div>
    </div>

    <div class="container">
        <fieldset>
            <legend>我的出行信息</legend>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-calendar"></i> 出发时间 <span render-html="travelTime">2018-07-11</span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-money"></i> 人均费用
                    <span render-html="perExpends"></span></div>
            </div>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-user-circle"></i> 人物
                    <span render-html="person"></span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-clock-o"></i> 出行天数
                    <span render-html="days"></span>
                </div>
            </div>
        </fieldset>

        <!--游记内容-->
        <div class="content" render-html="travelContent.content">
        </div>

        <hr>
        <!--评论模块-->
        <div class="container row comment">
            <div render-loop="list">
                <div>
                    <div class="col-2 commen t-head">
                        <a render-fun="getHref" render-key="list.author.id" data-href="/userProfiles.html?id=">
                            <img class="rounded-circle" render-src="list.author.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right" id="commenter">
                        <p class="authorName"><span render-html="list.author.nickName"></span></p>
                        <span class="comment-date" render-html="list.createTime"></span>
                        <div>
                            <a href="strategyComment.html">
                                <p render-html="list.content" class="commentText">
                                </p>
                            </a>
                            <div class="d-flex icon">
                                <a render-fun="getHref" render-key="list.id" data-href="/replyTravel.html?id=">
                                    <i class="fa  fa-commenting-o " style="margin-right: 10px"> <i render-html="list.commentNums">0</i>&nbsp回复 </i>
                                    <i class="fa fa-thumbs-o-up"> <span>0</span> </i>
                                </a>
                            </div>
                        </div>
                    </div>
                    <hr/>
                </div>
            </div>
        </div>
        <!--评论结束-->
    </div>
</div>
<!--</div>

</div>-->

<hr>
<div class="nums container d-flex operationBottom">
    <div class="p-2  flex-fill">
        <i class="fa fa-thumbs-o-up handleBtn" id="likeBtn" style="color: red" render-html="goodNum"></i>
    </div>
    <div class="p-2  flex-fill">
        <i class="fa  fa-commenting-o handleBtn" id="commentBtn" render-html="total"> </i>
    </div>
    <div class="p-2  flex-fill collectBtn">
        <i class="fa fa-heart-o handleBtn" id="collectBtn" render-html="collectNum"></i>
    </div>
</div>
<!--operationBottom-->
<div class="bg modal" id="travelModal" tabindex="-1" role="dialog">
    <div class="container tag" style="margin-top: 10px;">
        <textarea name="content" class="form-control" rows="5" placeholder="发表你的评价"></textarea>
        <div style="float: right;">
            <div id="remove"><span style="position: relative;left: 10px;">取消</span></div>
            <div id="submitBtn"><span style="position: relative;left: 10px;">发表</span></div>
        </div>
        <div style="height:45px;">
        </div>
    </div>
</div>
</body>

</html>